//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(";");
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == " ") {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

axios.defaults.timeout = 5000; // 超时时间
axios.defaults.baseURL = "http://localhost:8140"; // baseURL
//请求拦截器
axios.interceptors.request.use(function (config) {
    console.log(config);
    config.withCredentials = false;
    //设置jwt令牌
    config.headers.token = getCookie("token"); //sessionStorage.getItem("token")
    return config;
});
//响应拦截器
axios.interceptors.response.use((res) => {
    return res.data;
});

// 配置工具对象【字面量对象】
const jiandao = {
    //解析url获取参数的工具函数
    urlToObj(url) {
        url = url.split("?")[1];
        var ary = url.split("&");
        var obj = {};
        ary.forEach((item) => {
            var a = item.split("="); //["course1","js"]
            obj[a[0]] = a[1];
        });
        return obj;
    },
    pageTotal(total, size) {
        if (total == null || total == "") {
            return 0;
        } else {
            if (size != 0 && total % size == 0) {
                return parseInt(total / size);
            }
            if (size != 0 && total % size != 0) {
                return parseInt(total / size) + 1;
            }
        }
    },

    http: axios,
};
